<template>
<div>
    <Form :model="formCustom" :label-width="80">
        <FormItem label="ID" prop="id">
            <Input type="text" v-model="formCustom.id" :disabled="true"></Input>
        </FormItem>
        <FormItem label="商品类型" prop="t">
            <Input type="text":disabled="true" v-model="reqBody.typeValue"></Input>
        </FormItem>
        <FormItem label="商品名称" prop="name">
            <Input type="text" v-model="formCustom.name" ></Input>
        </FormItem>
        <FormItem label="商品类别" prop="category_id">
            <Select v-model="formCustom.category_id">
                <Option :value="item.id" v-for="item in formCustom.categorys">{{item.value}}</Option>
            </Select>
        </FormItem> 
        <FormItem label="图片" prop="head" v-for="i in imgList">
            <img  :src="i" width="50" height="50"/>
        </FormItem>
        <FormItem label="商品简介" prop="desc">
            <Input type="textarea" v-model="formCustom.intro" ></Input>
        </FormItem>
        <FormItem label="发布时间" >
            <Input type="text" v-model="timeValue" :disabled="true"></Input>
        </FormItem> 
        <FormItem label="堂豆数" prop="bean">
            <Input type="text" v-model="formCustom.bean" ></Input>
        </FormItem>
        <FormItem label="是否租赁" prop="is_rent">
            <Select v-model="formCustom.is_rent">
                <Option :value="0">不租赁</Option>
                <Option :value="1">租赁</Option>
            </Select>
        </FormItem>
        <FormItem label="租赁周期" prop="rent_period">
            <Input type="text" v-model="formCustom.rent_period" ></Input>
        </FormItem>
        <FormItem label="排序" prop="commodity_order">
            <Input type="text" v-model="formCustom.commodity_order" ></Input>
        </FormItem>
        <FormItem label="商品状态" prop="status">
            <Select v-model="formCustom.status">
                <Option :value="0">售出</Option>
                <Option :value="1">发布中</Option>
                <Option :value="2">待审核</Option>
                <Option :value="4">审核驳回</Option>
                <Option :value="5">待送出</Option>
                <Option :value="6">下架</Option>
                <Option :value="7">租赁中</Option>
            </Select>
        </FormItem>
       
        
        <FormItem>
            <Button type="primary" @click="handleSubmit">Submit</Button>
        </FormItem>
    </Form>
</div>
</template>

<script>
    import util from '../libs/util';
    export default {
        created() {
           this.reqBody = this.$route.query;
           this.loadData(this.reqBody);
        },
        data () {
            return {
                imgList: [],
                timeValue: '',
                formCustom : {},
                reqBody: {},
            }
        },
        methods: {

            loadData(reqBody) {
                var that = this;
                this.$http.post(util.baseURL+"commodity/getComodityInfo",reqBody,
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                               that.processData(response.data.data);
                            } else {
                               this.$Message.error("获取失败！");
                            }
                        },
                        (error)=>{
                            this.$Message.error("获取失败！");
                        }
                );
            },

            processData(data) {
                // 添加商品类别值
                if (this.reqBody.t == 1) {
                    this.reqBody.typeValue = "享堂集市";
                }
                if (this.reqBody.t == 2) {
                    this.reqBody.typeValue = "享堂学院";
                }
                if (this.reqBody.t == 3) {
                    this.reqBody.typeValue = "享堂书院";
                }
                // 图片处理
                var temp = data.head.split(";");
                this.imgList = [];
                for (var i=0; i<temp.length-1; i++) {
                    this.imgList.push(temp[i]);
                }
                // 时间处理
                this.timeValue = this.formatDateTime(data.time*1000);
                this.formCustom = data;
            }, 

            handleSubmit () {
              this.$http.post(util.baseURL+"commodity/updataMarketCommodity",this.formCustom,
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                               this.$Message.info("更新成功！");
                               this.$router.push("/index/marketcommoditylist");
                               this.formCustom = {};
                            } else {
                               this.$Message.error("更新失败！");
                            }
                        },
                        (error)=>{
                            this.$Message.error("更新失败！");
                        }
                );
            },

            formatDateTime(inputTime) {
                var date = new Date(inputTime);
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                m = m < 10 ? ('0' + m) : m;
                var d = date.getDate();
                d = d < 10 ? ('0' + d) : d;
                var h = date.getHours();
                h = h < 10 ? ('0' + h) : h;
                var minute = date.getMinutes();
                var second = date.getSeconds();
                minute = minute < 10 ? ('0' + minute) : minute;
                second = second < 10 ? ('0' + second) : second;
                return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
            },

        }
    }
</script>